﻿namespace HuaWei
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;

    public class FileCommon
    {
        public static void DeleteFileForced(string filename)
        {
            try
            {
                if (File.Exists(filename))
                {
                    File.SetAttributes(filename, FileAttributes.Normal);
                    File.Delete(filename);
                }
            }
            catch
            {
            }
        }

        public static byte[] GetBuffer(DataSet dataSet)
        {
            string tempFileName = Path.GetTempFileName();
            dataSet.WriteXml(tempFileName, XmlWriteMode.WriteSchema);
            return GetFileBuffer(tempFileName, true);
        }

        public static DataSet GetDataSet(byte[] dataSetBuff)
        {
            string tempFileName = Path.GetTempFileName();
            DataSet set = null;
            try
            {
                WriteFile(tempFileName, dataSetBuff);
                set = new DataSet();
                set.ReadXml(tempFileName, XmlReadMode.ReadSchema);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                try
                {
                    File.Delete(tempFileName);
                }
                catch
                {
                }
            }
            return set;
        }

        public static DataSet GetEncryptFile(string filename)
        {
            DataSet set = null;
            string tempFileName = Path.GetTempFileName();
            try
            {
                EncryptDecrypt.DecryptFile(filename, tempFileName);
                set = new DataSet();
                set.ReadXml(tempFileName, XmlReadMode.ReadSchema);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                try
                {
                    File.Delete(tempFileName);
                }
                catch
                {
                }
            }
            return set;
        }

        public static byte[] GetFileBuffer(string filename, bool delFile)
        {
            FileInfo info = new FileInfo(filename);
            byte[] buffer = new byte[info.Length];
            FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            try
            {
                stream.Read(buffer, 0, buffer.Length);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                stream.Close();
                try
                {
                    if (delFile)
                    {
                        File.Delete(filename);
                    }
                }
                catch
                {
                }
            }
            return buffer;
        }

        public static Image GetImage(Type type, string fileInAssembly)
        {
            Stream manifestResourceStream = Assembly.GetAssembly(type).GetManifestResourceStream(fileInAssembly);
            Image image = null;
            if (manifestResourceStream != null)
            {
                image = Image.FromStream(manifestResourceStream);
            }
            return image;
        }

        public static bool HasInValidateChar(string filename, out string errorMsg)
        {
            errorMsg = "";
            bool flag = false;
            char[] chArray = new char[] { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
            foreach (char ch in chArray)
            {
                if (filename.IndexOf(ch) >= 0)
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
            {
                errorMsg = "模块文件名不能包含下列任何字符之一:\r\n\\ / : * ? \" < > |";
            }
            return flag;
        }

        public static void WriteFile(string outputFile, byte[] data)
        {
            string directoryName = Path.GetDirectoryName(outputFile);
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            FileStream stream = null;
            try
            {
                stream = new FileStream(outputFile, FileMode.Create);
                stream.Write(data, 0, data.Length);
                stream.Flush();
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
    }
}

